<?php
/**
 * Copyright (c) 不争网络
 *
 * @Description     公共方法.
 * @Author          chenyanan
 * @Copyright       Copyright (c) 不争网络 保留所有版权()
 * @Date:           2018/5/4 0004
 * @CreateBy        PhpStorm.
 */
namespace app\api\controller;
use app\common\controller\Base;
use PHPQRCode\QRcode;
use think\Db;

class Pub extends Base{

    /**
     * 用户注册
     */
    public function dologin(){
        $model = model('common/user');

        $account = input('account');
        $validate = validate('common/send');
        if(!$validate->scene('check_code')->check($this->post)){
            $this->apiReturn(0,$validate->getError());
        }
    //验证码
        $res = model('common/send')->checkCode($this->post['account'],$this->post['code'],1);
        if($res['status'] == 0){
            $this->apiReturn(0,$res['msg']);
        }

        $user = $model->getUserInfo(['account'=>$this->post['account']]);
        if(empty($user)){
            do{
                $number = rand(100000000000,999999999999);
                $exists = $model->where('number',$number)->find();
            }while($exists);
            $data['account'] = $account;
            $data['number'] = $number;
            $data['add_time'] = time();
            $data['nickname'] = $number;
            $data['register_type'] = 0;
            $str = '';
            $char = 'abcdefgihjklmnopqistuvwxyz';
            for($i=0;$i<16;$i++){
                $str.= $char[mt_rand(0,strlen($char)-1)];
            }
            $str.= time();
            $data['token'] = str_shuffle($str);
            $query = model('common/user')->insert($data);
            $this->apiReturn(1,'登录成功',$model->getUserInfo(['number'=>$number]));
        }
        if($user['disabled'] == 0){
            $this->apiReturn(0,'该账号已被禁用');
        }
        $this->apiReturn(1,'登录成功',$user);
    }



    //发送短信
    public function sendCode(){
        
        $validate = validate('common/send');
        if(!$validate->scene('send')->check($this->post)){
            $this->apiReturn(0,$validate->getError());
        }

        if($this->post['type'] == 3 && !$user){
             $this->apiReturn(0,'该手机号未注册');
        }

        if($this->post['type'] == 2 && model('user')->getUserInfo(['account'=>$this->post['account']])!=''){
            $this->apiReturn(0,'手机号已被绑定');
        }
        switch($this->post['type']){
            case 1:$temp = config('ali_sms')['register_temp'];break;  //登录
            case 2:$temp = config('ali_sms')['register_temp'];break;  //绑定手机号 修改
            case 3:$temp = config('ali_sms')['register_temp'];break;  //忘记密码
        }
        $code = rand(1000,9999);

        // $res = ali_send($this->post['account'],["code"=>$code],$temp);

        // if(!$res['status'])
        //     $this->apiReturn(0,$res['msg']);

        //添加发送记录
        //$this->post['code'] = $code;
        $this->post['code'] = 123456;
        if(!model('common/send')->allowField(true)->save($this->post)){
            $this->apiReturn(0,'添加记录失败');
        }
        $this->apiReturn(1,'发送成功');
    }




    /**
     * 验证码校验
     */
    public function checkCode(){
        $validate = validate('common/send');
        if(!$validate->scene('check_code')->check($this->post)){
            $this->apiReturn(0,$validate->getError());
        }
        $res = model('common/send')->checkCode($this->post['account'],$this->post['code'],$this->post['type']);

        $this->apiReturn($res['status'],$res['msg']);
        
    }

    /**
     * 第三方登录
     */
    public function thirdLogin(){
        $model = model('common/user');

        do{
            $number = rand(100000000000,999999999999);
            $exists = $model->where('code',$codes)->find();
        }while($exists);


        $code = input('code','','trim');
        if($code == ''){
            $this->apiReturn(0,'参数code丢失');
        }
        $get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?';
        $get_token_url .= 'appid='.config('weixin_pay.app_id').'&secret='.config('weixin_pay.appsecret').'&code='.$code.'&grant_type=authorization_code';
        $res = file_get_contents($get_token_url);
        $obj = json_decode($res,true);
        $token = $obj['access_token'];
        $open_id = $obj['openid'];
        $info = model('User')->getUserInfo(['openid'=>$open_id]);
        if($info){
            $this->apiReturn(1,'登陆成功',$info);
        }
        $getuserinfo = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$token.'&openid='.$open_id;
        $user_info = json_decode(file_get_contents($getuserinfo),true);
        $data['nickname'] = $user_info['nickname'];
        $data['sex'] = $user_info['sex'];
        $data['photo_path'] = $user_info['headimgurl'];
        $data['openid'] = $open_id;
        $data['number'] = $number;
        $data['add_time'] = time();
        $data['register_type'] = 1;
    	$query = model('User')->save($data);
    	if($query){
    		$info = model('User')->getUserInfo(['openid'=>$open_id]);
        	$this->apiReturn(1,'登录成功',$info);
    	}else{
    		$this->apiReturn(0,'登录失败,请稍后再试');
    	}
    }


    /**
     * 忘记密码
     */
    public function changePwd(){
        $model = model('common/user');
        $user_validate = validate('common/user');
        $send_validate = validate('common/send');

        if(!$user_validate->scene('forget')->check($this->post)){
            $this->apiReturn(0,$user_validate->getError());
        }

        $this->post['type'] = 3;
        if(!$send_validate->scene('check_code')->check($this->post)){
            $this->apiReturn(0,$send_validate->getError());
        }

        $model = $model->getUserInfo(['account'=>$this->post['account']],'id');
        if(is_null($model))
            $this->apiReturn(0,'该账号不存在');
        $res = model('send')->checkCode($this->post['account'],$this->post['code'],3);
        if($res['status'] === 0){
            $this->apiReturn(0,$res['msg']);
        }

        if(!$model->allowField(true)->save(['password'=>$this->post['password']])){
            $this->apiReturn(0,'重置密码失败');
        }
        $user_info = $model->getUserInfo(['account'=>$this->post['account']]);
        $this->apiReturn(1,'重置密码成功');
    }

    public function upload(){
        $img = $this->uploads('evaluate','image');
    }
}